package com.jumper.study.front.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.util.AdminUtil;
import com.jumper.study.db.model.Account;
import com.jumper.study.front.dto.req.UpdateInfoReq;
import com.jumper.study.front.dto.resp.StudentInfo;
import com.jumper.study.front.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;



@RestController("frontStudentController")
@RequestMapping("/api/front/student")
public class StudentController {
    @Autowired
    private AdminUtil adminUtil;

    @Autowired
    private StudentService studentService;

    /**
     * 学员个人信息
     * @return
     */
    @GetMapping("/info")
    public CommonResult<StudentInfo> info() {
        Account account = adminUtil.getAccount();
        if (account == null) {
            return CommonResult.unauthorized(null);
        }

        StudentInfo info = studentService.getInfo(account.getId());
        if (info == null) {
            return CommonResult.failed("未注册");
        }
        
        return CommonResult.success(info);
    }

    /**
     * 修改个人信息
     * @param req
     * @return
     */
    @PostMapping("/info")
    public CommonResult<Boolean> updateInfo(@RequestBody @Validated UpdateInfoReq req) {
        Account account = adminUtil.getAccount();
        req.setAccountId(account.getId());
        boolean result = studentService.updateInfo(req);
        return CommonResult.success(result);
    }
    
}
